package com.jfplugin.mail.mockhttp;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by farmer on 16/8/31.
 *
 * @author 1256810099@qq.com
 */
public class MockHttpServletRequest implements InvocationHandler {

    private final Map<Object, Object> dataMap = new HashMap<>();

    interface MethodName {
        String GET_ATTRIBUTE_NAMES = "getAttributeNames";
        String SET_ATTRIBUTE = "setAttribute";
        String GET_ATTRIBUTE = "getAttribute";
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) {
        if (MethodName.GET_ATTRIBUTE_NAMES.equals(method.getName())) {
            return Collections.enumeration(dataMap.keySet());
        } else if (MethodName.SET_ATTRIBUTE.equals(method.getName())) {
            return dataMap.put(objects[0], objects[1]);
        } else if (MethodName.GET_ATTRIBUTE.equals(method.getName())) {
            return dataMap.get(objects[0]);
        }
        return null;
    }

}